package editAir.editContent
{
	import _Pan3D.core.MathCore;
	
	import _me.Scene_data;
	
	import editAir.buildEdit.EditManage;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;

	public class FileListView extends MovieClip
	{
		private var _w:int=270;
		private var _h:int=420;
		private var _list:ModelList;
		private var _fileItem:Array;
		private var _showBitmap:Bitmap;
		private var _lastMouseDownX:Number;
		private var _listTitleBar:ListTitleBar;
		private var _inputModeSprite:InputModeSprite;
		private var _outItemBut:MovieClip;
		private var _openItemBut:MovieClip;
		private var _showXmlTxt:TextField;


		public function FileListView()
		{
			addBacks();
			addinputModeSprite();
			addTexts();
			addLists();
			addButs();
			addMoveBitmap();
			addEvents();
		}

		private function addButs():void
		{
			_outItemBut=ModelUiManege.getClass("pic_image_7");
			_outItemBut.x=50+3
			_outItemBut.y=15+3
			addChild(_outItemBut);

			_openItemBut=ModelUiManege.getClass("pic_image_6");
			_openItemBut.x=20+3
			_openItemBut.y=15+3
			addChild(_openItemBut);

		}

		private function addinputModeSprite():void
		{
			_inputModeSprite=new InputModeSprite;
			addChild(_inputModeSprite);
			_inputModeSprite.x=10;
			_inputModeSprite.y=385;
		}

		private function addMoveBitmap():void
		{
			_showBitmap=new Bitmap;
			_showBitmap.visible=false;
			addChild(_showBitmap);
			_showBitmap.x=300;
		}

		private function addEvents():void
		{
			ModelUiManege.stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUp)
			ModelUiManege.stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMove)
			this.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel)
			_outItemBut.addEventListener(MouseEvent.CLICK, outItemButClik)
			_openItemBut.addEventListener(MouseEvent.CLICK, openItemButClick)
			changeSize();

		}

		public function changeSize():void
		{
			_inputModeSprite.y=_list.y + _list.h + 15;
			_backPic.width=270;
			_backPic.height=_inputModeSprite.y + 60;
		}


		private var loadFile:FileReference;
		private var _backPic:Shape;

		private function openItemButClick(event:MouseEvent):void
		{
			loadFile=new FileReference();
			loadFile.addEventListener(Event.SELECT, onSel);
			loadFile.addEventListener(Event.COMPLETE, onLoadCom);
			loadFile.browse();
		}

		private function onSel(event:Event):void
		{
			loadFile.load();
		}

		private function onLoadCom(event:Event):void
		{
			var byte:ByteArray=event.target.data as ByteArray;
			byte.position=0;
			var str:String=byte.readUTFBytes(byte.bytesAvailable);
			upDataXml(XML(str));
		}

		private function loadXmlData(url:String):void
		{
			//加载地面XML列表
			var _loaderURL:URLRequest=new URLRequest(Scene_data.fileRoot + "build/buildeditstatList.xml");
			var _scenexml:URLLoader=new URLLoader();
			_scenexml.load(_loaderURL);
			_scenexml.addEventListener(Event.COMPLETE, _loadGroundXmlOk);
			function _loadGroundXmlOk(event:Event):void
			{
				upDataXml(XML(_scenexml.data));
			}
		}

		protected function outItemButClik(event:MouseEvent):void
		{

			//	<ico><id>4</id><father>0</father><filetype>1</filetype><open>1</open><name>id102</name>
			//	<angley>54</angley><type>3</type><et>box512</et><scale>1</scale><x>400</x><y>0</y><z>500</z></ico>
			var xmlStr:String="<list>"

			for (var i:int=0; i < _fileItem.length; i++)
			{

				var tempModelVo:ModelVO=_fileItem[i];
				if (tempModelVo)
				{

					var tempXml:String="";
					tempXml+="<ico>";
					tempXml+="<id>" + tempModelVo.id + "</id>";
					tempXml+="<father>" + tempModelVo.father + "</father>";
					tempXml+="<filetype>" + tempModelVo.filetype + "</filetype>";
					tempXml+="<open>" + String(tempModelVo.open ? 1 : 0) + "</open>";
					tempXml+="<name>" + tempModelVo.name + "</name>";
					tempXml+="<angley>" + tempModelVo.angle_y + "</angley>";
					tempXml+="<type>" + tempModelVo.type + "</type>";
					tempXml+="<et>" + tempModelVo.et + "</et>";
					tempXml+="<scale>" + tempModelVo.scale + "</scale>";
					tempXml+="<x>" + tempModelVo.x + "</x>";
					tempXml+="<y>" + tempModelVo.y + "</y>";
					tempXml+="<z>" + tempModelVo.z + "</z>";
					tempXml+="</ico>";
					trace(tempXml);
					xmlStr+=tempXml
				}
			}
			xmlStr+="</list>"
			//_showXmlTxt.text=xmlStr;
			_showXmlTxt.visible=!_showXmlTxt.visible;

			_showXmlTxt.x=-this.x
			_showXmlTxt.y=-this.y
			var file:FileReference=new FileReference;
			var xml:XML=XML(xmlStr)
			file.save(xml, "buildildeditstatList.xml");
		}

		protected function onMouseWheel(event:MouseEvent):void
		{
			_list.listScroll.speedNum(-event.delta);

		}

		protected function stageMouseMove(event:MouseEvent=null):void
		{
			if (ModelUiManege.isMoveFileNow) 
			{
				_showBitmap.x=mouseX - _lastMouseDownX + 20;
				_showBitmap.y=mouseY - 15;
				_showBitmap.visible=true;

				//将要移动的文件，半透明

				_list.setMoveAllCellAphal(ModelUiManege.isMoveModelVo.id)
			}
		}

		protected function stageMouseUp(event:MouseEvent):void
		{
			_showBitmap.visible=false;
			ModelUiManege.isMoveFileNow=false;
			_list.setMoveAllCellAphal(99999)
		}

		public function changeModelVoFather(fatherId:uint):void
		{

			if (ModelUiManege.isMoveModelVo && _showBitmap.visible)
			{
				if (ModelUiManege.isMoveModelVo.id != fatherId)
				{
					var canMove:Boolean=_list.isItSonAndCamMove(ModelUiManege.isMoveModelVo.id, fatherId)
					if (canMove)
					{
						ModelUiManege.isMoveModelVo.father=fatherId;
						_list.changePasition()
					}
				}
			}
		}

		public function moveModelvo(vo:ModelVO):void
		{

			var tempBitMapData:BitmapData=new BitmapData(300, vo.bh, true, 0x000000);
			var m:Matrix=new Matrix;
			m.ty=-vo.by;
			_list.allCell.mask=null;
			tempBitMapData.draw(_list.allCell, m);
			_showBitmap.bitmapData=tempBitMapData;
			_list.allCell.mask=_list.maskMc;
			_lastMouseDownX=mouseX;
			ModelUiManege.isMoveFileNow=true;
			ModelUiManege.isMoveModelVo=vo;

		}

		private function addLists():void
		{
			_list=new ModelList();
			addChild(_list);
			_list.x=10;
			_list.y=40;
			_listTitleBar=new ListTitleBar;
			_listTitleBar.x=10;
			_listTitleBar.y=10;
			addChild(_listTitleBar);
			ModelUiManege.fileList=_list
		}

		private function addTexts():void
		{
			var titleTxt:TextField=FileTextUtil.getTextField();
			titleTxt.htmlText="场景模型列表";
			addChild(titleTxt);
			titleTxt.x=15;
			titleTxt.y=15;



			var thistextformat:TextFormat=new TextFormat;
			_showXmlTxt=new TextField()
			_showXmlTxt.defaultTextFormat=thistextformat;
			_showXmlTxt.htmlText="ccav";

			_showXmlTxt.autoSize=TextFieldAutoSize.LEFT;
			_showXmlTxt.width=600;
			//_showXmlTxt.type = TextFieldType.INPUT;
			_showXmlTxt.x=-750;
			_showXmlTxt.y=-50;
			addChild(_showXmlTxt);
			_showXmlTxt.visible=false;


		}

		public function upDataXml(xml:XML):void
		{
			ModelUiManege.fileList.clearAll();
			EditManage.buildView.clearAll();
			_fileItem=new Array;
			var _item:Array=MathCore.getSourcePro(xml);
			for (var i:int=0; i < _item.length; i++)
			{
				var tempModelVo:ModelVO=new ModelVO;
				tempModelVo.name=_item[i].name;
				tempModelVo.id=_item[i].id;
				tempModelVo.et=_item[i].et;
				tempModelVo.x=_item[i].x;
				tempModelVo.y=_item[i].y;
				tempModelVo.z=_item[i].z;
				tempModelVo.scale=_item[i].scale;
				tempModelVo.angle_y=Number(_item[i].angley);
				tempModelVo.open=(_item[i].open == 1);
				tempModelVo.filetype=_item[i].filetype;
				tempModelVo.father=_item[i].father;
				tempModelVo.bh=DataType.NUM25
				_fileItem.push(tempModelVo)
			}
			_list.source(_fileItem);
		}

		private function addBacks():void
		{

			_backPic=new Shape;
			addChild(_backPic);
			_backPic.graphics.beginFill(0x003c00, 1);
			_backPic.graphics.drawRect(0, 0, 250 + 20, 320 + 45);
			_backPic.graphics.endFill();


		}

		public function get inputModeSprite():InputModeSprite
		{
			return _inputModeSprite;
		}



	}
}
